#coding=utf-8

import sys
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib

url = "http://v2245.ants-city.com/"
#headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0','Referer' : 'http://v2245.ants-city.com/Account/Login?ReturnUrl=%2f'}

filename = 'cookie6.txt'

cookie = cookielib.LWPCookieJar(filename)
try:
    cookie.load(filename)
except Exception as e:
    pass
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
result = opener.open(url)
print result.read()

print('=============================')
cookie.save(ignore_discard=True, ignore_expires=True)

v = ''
for item in cookie:
    if(item.name == '__RequestVerificationToken'):
        v = item.value



loginUrl = 'http://v2245.ants-city.com/Account/Login?ReturnUrl=%2F'

data = {
    'Company' : '万加',
    'Password' : 'iwilley',
    'UserName' : 'manager',
    '__RequestVerificationToken' : v,
}

cookie5 = cookielib.LWPCookieJar(filename)
cookie5.load(filename)
opener5 = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie5))
postdata = urllib.urlencode(data)
result = opener5.open(loginUrl,postdata)
cookie5.save(ignore_discard=True, ignore_expires=True)


print('cookie load')
print(cookie5)

v = ''
for item in cookie5:
    print(item.value)


#http://v2245.ants-city.com/DRP/DRPFItemStockReport/GetData?action=read&page=1&start=0&limit=30&sort=[{%22property%22%3A%22Available%22%2C%22direction%22%3A%22DESC%22}]&filter=[{%22property%22%3A%22ItemInfo%22%2C%22value%22%3A%22111%22}]
url_req = 'http://v2245.ants-city.com/DRP/DRPFItemStockReport/GetData?action=read&page=1&start=0&limit=30&sort=[{%22property%22%3A%22Available%22%2C%22direction%22%3A%22DESC%22}]&filter=[{%22property%22%3A%22ItemInfo%22%2C%22value%22%3A%22111%22}]'

result5 = opener5.open(url_req)
print(result5)


